<template>
  <div class="bg-white m-4 mr-0 overflow-hidden">
    <BasicTree
      title="部门列表"
      toolbar
      search
      :clickRowToExpand="false"
      :treeData="treeData"
      :replaceFields="{ key: 'id', title: 'displayName' }"
      @select="handleSelect"
    />
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';

  import { BasicTree } from '/@/components/Tree';
  import { getDeptAllList } from '/@/api/sys/system';
  import { listToTree } from '/@/utils/helper/treeHelper';
  import { DeptListItem } from '/@/api/sys/model/systemModel';
  export default defineComponent({
    name: 'DeptTree',
    components: { BasicTree },

    emits: ['select'],
    setup(_, { emit }) {
      const treeData = ref<DeptListItem[]>([]);

      async function fetch() {
        const temp_data = await getDeptAllList();
        treeData.value = listToTree(temp_data, { id: 'id', pid: 'parentId' });
      }

      function handleSelect(keys) {
        emit('select', keys[0]);
      }

      onMounted(() => {
        fetch();
      });
      return { treeData, handleSelect };
    },
  });
</script>
